$(() => {
    const token = GetQueryString("token");
    // 获取area_id
    let id = GetQueryString('id');

    $.ajax({
        type: "post",
        url: url,
        data: { 'method': "Goods|goods_detail", 'id': id, 'token': token },
        dataType: 'json',
        success: function(res) {
            // console.log(res);
            if (res.status == 1) {
                let html = '';
                let banner = '';
                let shop = '';
                let company = res.data.company;
                let goods = res.data.goods;

                // 客服
                $('.contactBtn').attr('data-uid', company.hx_uid);

                // 分享
                let shareUrl = domain + "/view/share_A_goodsDetail.html?id=" + goods.id;
                let shareTitle = goods.goods_name;
                let shareDes = goods.description;
                let shareImgUrl = goods.goods_img;

                $('.share').on('click', function() {
                    if (isAndroid_ios()) {
                        //安卓  
                        window.android.shareWebMethod(shareUrl, shareTitle, shareDes, shareImgUrl);
                    } else {
                        //ios
                        // window.webkit.messageHandlers.shareWebMethod.postMessage(shareUrl, shareTitle, shareDes, shareImgUrl);
                        var d = {};
                        d.shareUrl = shareUrl;
                        d.title = shareTitle;
                        d.description = shareDes;
                        d.imgUrl = shareImgUrl;
                        window.webkit.messageHandlers.shareWebMethod.postMessage(d);
                    }
                })

                // 商品名称
                $('.hetitle').html(goods.goods_name);
                $('.detailShop').html(`<img src="${goods.description_img}"/>`);
                shop += `
                    <div class="headlineText" style="margin-bottom: .1rem;">${goods.description}</div>`
                if (goods.price_show_model == 1) {
                    shop += `<p class="redPrice">¥${goods.goods_price}</p>`;
                } else if (goods.price_show_model == 2) {
                    shop += `<p ><del style="color:#666;font-size:.24rem;">¥${goods.price_before}</del>/<span class="redPrice" style="font-size:.28rem;">¥${goods.goods_price}</span></p>`;
                } else {
                    shop += `<p class="redPrice">¥${goods.price_area}</p>`;
                }
                if (company.company_level >= 1) {
                    html += `<a href="shop_details_company.html?company_id=${company.id}&token=${token}" class="comHead">`
                } else {
                    html += `<a href="shop_details.html?company_id=${company.id}&token=${token}" class="comHead">`
                }
                html += `<div class="comHeadLeft">
                            <img class="headImg" src="${company.company_logo == '' ? '../img/user.jpg' : company.company_logo }" alt="${company.company_name}">
                            <div class="headlineTopChild">
                                <p>${company.company_name}</p>`
                if (company.level_logo != '') {
                    html += `<img src="${company.level_logo}" class="userLevel" alt="${company.level_name}"/>`
                }
                html += `</div>
                        </div>
                        <p class="comHeadRight">进店</p>
                        </a>
                    <p class="mainDesc">主营：${company.main_business}</p>
                `;
                $('.shopDetailText').html(shop);
                $('.headWrap').html(html);

                goods.pic_list.forEach(item => {
                    banner += `
                        <div class="swiper-slide"><img data-id="${item.id}" src="${item.thumb_url == '' ? '../img/banner.png' : item.thumb_url}" alt="${item.pic_name}" class="baimg plusBanner" /></div>
                    `;
                })
                $('.adBanner .swiper-wrapper').html(banner);
                // banner轮播
                if (goods.pic_list.length == 1) {
                    new Swiper('.adBanner', {
                        autoplay: 3000,
                        // pagination: '.swiper-pagination',
                        loop: false
                    })
                } else {
                    new Swiper('.adBanner', {
                        autoplay: 3000,
                        // pagination: '.swiper-pagination',
                        loop: true
                    })
                }
                // 图片放大轮播
                $('.plusBanner').on('click', function() {
                    dialog('#plusBanner');
                    let html = '';
                    goods.pic_list.forEach(item => {
                        html += `
                        <div class="swiper-slide">
                            <img class="showImg" src="${item.pic_url}" alt="">
                        </div>
                    `;
                    })
                    $('.plusBannerSwiper .swiper-wrapper').html(html);
                    // banner轮播
                    new Swiper('.plusBannerSwiper', {
                        // pagination: '.swiper-pagination',
                        paginationType: 'fraction',
                        loop: true
                    })
                })

                // 收藏商品状态
                $('.collect_btn').attr('data-collect', goods.has_follow);
                if (goods.has_follow == 1) {
                    $('.collect').attr('src', '../img/shopnapic1_on.png');
                } else {
                    $('.collect').attr('src', '../img/shopnapic1.png');
                }

                // 点击收藏
                $('.collect_btn').on('click', function() {
                    let goods_id = goods.id;
                    let collect = $(this).attr('data-collect');
                    console.log(collect);
                    if (collect == 1) {
                        // 取消关注
                        $.ajax({
                            type: "post",
                            url: url,
                            data: { 'method': "User|goods_favorite_del", 'token': token, 'goods_id': goods_id },
                            dataType: 'json',
                            success: function(res) {
                                // console.log(res);
                                if (res.status == 1) {
                                    $('.collect_btn').attr('data-collect', 0);
                                    $('.collect').attr('src', '../img/shopnapic1.png');
                                } else if (res.status == 2) {
                                    location.href = "fox://logout";
                                } else {
                                    alert(res.message);
                                }
                            }
                        })
                    } else {
                        // 关注
                        $.ajax({
                            type: "post",
                            url: url,
                            data: { 'method': "User|goods_favorite", 'token': token, 'goods_id': goods_id },
                            dataType: 'json',
                            success: function(res) {
                                // console.log(res);
                                if (res.status == 1) {
                                    $('.collect_btn').attr('data-collect', 1);
                                    $('.collect').attr('src', '../img/shopnapic1_on.png');
                                } else if (res.status == 2) {
                                    location.href = "fox://logout";
                                } else {
                                    alert(res.message);
                                }
                            }
                        })
                    }

                })

                // 进店逛逛
                $('.go_shop').on('click', function() {
                    if (company.company_level >= 1) {
                        location.href = "shop_details_company.html?company_id=" + company.id + "&token=" + token;
                    } else {
                        location.href = "shop_details.html?company_id=" + company.id + "&token=" + token;
                    }
                })
            } else if (res.status == 2) {
                location.href = "fox://logout";
            } else {
                alert(res.message);
            }
        }
    })

    // 联系客服
    $('.contactBtn').on('click', function() {
        let uid = $(this).attr('data-uid');
        if (uid != '') {
            if (isAndroid_ios()) {
                //安卓  
                window.android.startChat(uid);
            } else {
                //ios
                window.webkit.messageHandlers.startChat.postMessage(uid);
            }
        } else {
            alert('暂无客服信息！！！')
        }
    })

    // 分享下载链接及备案信息底部
    if ($('.downLoad').length) {
        $.ajax({
            type: "post",
            url: url,
            data: { "method": "Api|share_bottom" },
            dataType: 'json',
            success: function(res) {
                if (res.status == 1) {
                    let data = res.data;
                    let html = '';
                    data.download.forEach(item => {
                        html += `
                <li><a href="${item.url}">${item.type_name}</a></li>
                `;
                    })
                    $('.dlLists').html(html);
                    $('.icp_info').html(data.icp_info);
                    $('.other_msg').html(data.other_msg);
                }
            }
        })
    }
})